﻿System.register(['angular2/core', 'angular2/http', '../domain/todo'], function(exports_1) {
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __metadata = (this && this.__metadata) || function (k, v) {
        if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
    };
    var core_1, http_1, todo_1;
    var TodoStore;
    return {
        setters:[
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (http_1_1) {
                http_1 = http_1_1;
            },
            function (todo_1_1) {
                todo_1 = todo_1_1;
            }],
        execute: function() {
            TodoStore = (function () {
                function TodoStore(http) {
                    var _this = this;
                    this.http = http;
                    this._baseUri = 'http://localhost:53990/api/todo';
                    this.topics = [];
                    this.todos = [];
                    this.http.get(this._baseUri)
                        .subscribe(function (res) {
                        var data = res.json();
                        _this.topics = data.map(function (todo) {
                            var ret = new todo_1.Todo(todo.Title);
                            ret.Id = todo.Id;
                            ret.GroupName = todo.GroupName;
                            ret.ParentId = todo.ParentId;
                            ret.SortOrder = todo.SortOrder;
                            ret.Completed = todo.Completed;
                            return ret;
                        });
                    }, function (error) {
                        if (error.status == 401) {
                        }
                        console.log('Error: ' + error);
                    }, function () {
                        _this._currentTopic = _this.topics[0].Id;
                        _this.http.get(_this._baseUri + "?completed=" + false + "&parentId=" + _this._currentTopic)
                            .subscribe(function (res) {
                            var data = res.json();
                            _this.todos = data.map(function (todo) {
                                var ret = new todo_1.Todo(todo.Title);
                                ret.Id = todo.Id;
                                ret.GroupName = todo.GroupName;
                                ret.ParentId = todo.ParentId;
                                ret.SortOrder = todo.SortOrder;
                                ret.Completed = todo.Completed;
                                return ret;
                            });
                        }, function (error) {
                            if (error.status == 401) {
                            }
                            console.log('Error: ' + error);
                        }, function () { return console.log("Success"); });
                    });
                }
                TodoStore.prototype.get = function (completed, id) {
                    var _this = this;
                    var url = this._baseUri + "?completed=" + completed;
                    if (id) {
                        this._currentTopic = id;
                        url = url + "&parentId=" + id;
                    }
                    else
                        url = url + "&parentId=" + this._currentTopic;
                    console.log(url);
                    return this.http.get(url)
                        .subscribe(function (res) {
                        var data = res.json();
                        _this.todos = data.map(function (todo) {
                            var ret = new todo_1.Todo(todo.Title);
                            ret.Id = todo.Id;
                            ret.GroupName = todo.GroupName;
                            ret.ParentId = todo.ParentId;
                            ret.SortOrder = todo.SortOrder;
                            ret.Completed = todo.Completed;
                            return ret;
                        });
                    }, function (error) {
                        if (error.status == 401) {
                        }
                        console.log('Error: ' + error);
                    }, function () { return console.log("Success"); });
                };
                TodoStore.prototype.allCompleted = function () {
                    return this.todos.length === this.getCompleted().length;
                };
                TodoStore.prototype.setAllTo = function (completed) {
                    for (var i = 0, len = this.todos.length; i < len; i++) {
                        var t = this.todos[i];
                        t.Completed = false;
                        this.toggleCompletion(t);
                    }
                };
                TodoStore.prototype.removeCompleted = function () {
                    this.todos = this.getWithCompleted(false);
                };
                TodoStore.prototype.getRemaining = function () {
                    return this.getWithCompleted(false);
                };
                TodoStore.prototype.getCompleted = function () {
                    return this.getWithCompleted(true);
                };
                TodoStore.prototype.update = function (todo) {
                    var headers = new http_1.Headers({ 'Content-Type': 'application/json' });
                    this.http.put(this._baseUri + "/" + todo.Id.toString(), JSON.stringify(todo), { headers: headers })
                        .subscribe(function (res) {
                        console.log("!");
                    }, function (error) { return console.log('There was an error: ' + error); });
                };
                TodoStore.prototype.toggleCompletion = function (todo) {
                    var _this = this;
                    todo.Completed = !todo.Completed;
                    console.log(todo.Completed);
                    var headers = new http_1.Headers({ 'Content-Type': 'application/json' });
                    this.http.put(this._baseUri + "/" + todo.Id.toString(), JSON.stringify(todo), { headers: headers })
                        .subscribe(function (res) {
                        _this.todos.splice(_this.todos.indexOf(todo), 1);
                        console.log("!");
                    }, function (error) { return console.log('There was an error: ' + error); });
                };
                TodoStore.prototype.remove = function (todo) {
                    var _this = this;
                    if (todo.Id != 0) {
                        return this.http.delete(this._baseUri + "/" + todo.Id.toString())
                            .subscribe(function (res) {
                            _this.todos.splice(_this.todos.indexOf(todo), 1);
                            console.log(res.json());
                        }, function (error) { return console.log('There was an error: ' + error); });
                    }
                };
                TodoStore.prototype.add = function (title) {
                    var _this = this;
                    var todo = new todo_1.Todo(title);
                    todo.ParentId = this._currentTopic;
                    var headers = new http_1.Headers({ 'Content-Type': 'application/json' });
                    this.http.post(this._baseUri, JSON.stringify(todo), { headers: headers })
                        .subscribe(function (res) {
                        console.log(res.json());
                        _this.todos.unshift(res.json());
                    }, function (error) { return console.log('There was an error: ' + error); });
                };
                TodoStore.prototype.getWithCompleted = function (completed) {
                    return this.todos.filter(function (todo) { return todo.Completed === completed; });
                };
                TodoStore = __decorate([
                    core_1.Injectable(), 
                    __metadata('design:paramtypes', [http_1.Http])
                ], TodoStore);
                return TodoStore;
            })();
            exports_1("TodoStore", TodoStore);
        }
    }
});
//# sourceMappingURL=store.js.map